namespace Synchronization
{
    internal class Synchronization
    {
        public void Synch(string pSourceFullPath, string pTargetFullPath)
        {
            Console.WriteLine($"pSourceFullPath:{pSourceFullPath}");
            Console.WriteLine($"pTargetFullPath:{pTargetFullPath}");

            if (!Directory.Exists(pSourceFullPath))
            {
                Console.WriteLine($"源目录不存在 pSourceFullPath:{pSourceFullPath}");
                return;
            }

            if (Directory.Exists(pTargetFullPath))
            {
                string[] files = Directory.GetFiles(pTargetFullPath);
                if (files.Length > 0)
                    Directory.Delete(pTargetFullPath, true);
            }

            CopyDirectory(pSourceFullPath, pTargetFullPath);

            Console.WriteLine();
        }

        private void CopyDirectory(string pSourcePath, string pTargetFullPath)
        {
            if (!Directory.Exists(pTargetFullPath))
            {
                Directory.CreateDirectory(pTargetFullPath);
                Console.WriteLine($"创建目录 pTargetFullPath:{pTargetFullPath}");
            }

            string[] directories = Directory.GetDirectories(pSourcePath);
            foreach (string sourcePath in directories)
            {
                string destPath = sourcePath.Replace(pSourcePath, pTargetFullPath);
                CopyDirectory(sourcePath, destPath);
            }

            string[] files = Directory.GetFiles(pSourcePath);
            foreach (string file in files)
            {
                string targetFile = file.Replace(pSourcePath, pTargetFullPath);
                File.Copy(file, targetFile, true);
                Console.WriteLine($"拷贝文件 file:{file}");
            }
        }
    }
}
